<template>
  <div class="app-container">
    <div class="filter-container clearfix">
      <el-input v-model="listQuery.actor_id" class="filter-item" style="width: 160px;" placeholder="角色ID" />
      <el-button class="filter-item" type="primary" plain icon="el-icon-search" @click="handleFilter">搜索</el-button>
      <el-button v-if="checkPermission(permissionRoles.vip_list_add)" class="filter-item" type="primary" icon="el-icon-plus" @click="handleAdd">添加角色ID</el-button>
    </div>
    <el-table ref="table" v-loading="listLoading" :data="list" border stripe style="width: 100%;" height="670">
      <el-table-column label="服务器" prop="sname" align="center"></el-table-column>
      <el-table-column label="账号" prop="account" width="110px" align="center"></el-table-column>
      <el-table-column label="角色ID" prop="actor_id" width="110px" align="center"></el-table-column>
      <el-table-column label="角色名" prop="actor_name" align="center"></el-table-column>
      <el-table-column label="等级" prop="level" align="center"></el-table-column>
      <el-table-column label="VIP等级" prop="vip" align="center"></el-table-column>
      <el-table-column label="充值金额" prop="amount" width="100px" align="center"></el-table-column>
      <el-table-column label="战力" prop="power" align="center"></el-table-column>
      <el-table-column label="钻石" prop="diamond" width="110px" align="center"></el-table-column>
      <el-table-column label="创角时间" prop="create_dt" width="160px" align="center"></el-table-column>
      <el-table-column label="最后离线时间" prop="last_logout_dt" width="160px" align="center"></el-table-column>
      <el-table-column label="离线天数" prop="offline_days" width="110px" align="center"></el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="{row}">
          <el-button v-if="checkPermission(permissionRoles.vip_list_del)" type="text" size="small" title="删除" @click="handleDelete(row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog :visible.sync="dialogVisible" title="添加角色" :close-on-click-modal="false">
      <el-form ref="dataForm" :model="dataForm" label-width="100px" label-position="left">
        <el-form-item label="角色ID">
          <el-input v-model="dataForm.actor_id" type="textarea" :autosize="{ minRows: 7, maxRows: 10}" />
        </el-form-item>
      </el-form>
      <div class="tr">
        <el-button type="danger" @click="dialogVisible=false">取消</el-button>
        <el-button type="primary" @click="confirmSubmit()">确定</el-button>
      </div>
    </el-dialog>
    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />
  </div>
</template>

<script>
import checkPermission from '@/utils/permission' // 权限判断函数
import waves from '@/directive/waves' // 实现点击波浪纹
import Pagination from '@/components/Pagination' // secondary package based on el-pagination
import { handleVip, getVipList, deleteVip } from '@/api/operator'

export default {
  name: 'VipList',
  components: { Pagination },
  directives: { waves },
  data() {
    return {
      permissionRoles: this.$store.state.user.permissionRoles,
      list: [],
      total: 0,
      loading: false,
      listLoading: false,
      dataForm: {
        actor_id: ''
      },
      listQuery: {
        actor_id: '',
        page: 1,
        limit: 200
      },
      dialogVisible: false
    }
  },
  created() {
    this.getList()
  },
  methods: {
    checkPermission,
    async getList() {
      this.listLoading = true
      const res = await getVipList(this.listQuery)
      this.listLoading = false
      this.list = res.data.items
      this.total = res.data.total
    },
    handleFilter() {
      this.getList()
    },
    handleAdd() {
      this.dataForm.actor_id = ''
      this.dialogVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    confirmSubmit() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          handleVip(this.dataForm).then(response => {
            if (response.status) {
              this.dialogVisible = false
              this.getList()
              this.$message({
                type: 'success',
                message: '操作成功!'
              })
            } else {
              this.message.error(response.message)
            }
          })
        }
      })
    },
    handleDelete(row) {
      this.$confirm('你确定要删除此行数据吗?', '删除', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async() => {
          await deleteVip(row.actor_id)
          this.getList()
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        })
        .catch(err => { console.error(err) })
    }
  }
}
</script>
